<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./js/jquery-1.12.4.min.js"></script>
    <link rel="stylesheet" href="./图书列表案例/lib/bootstrap.css">
</head>

<body>

    <body style="padding: 15px;">

        <!-- 评论面板 -->
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">发表评论</h3>
            </div>
            <form class="panel-body" id="formAddCmt">
                <div>评论人：</div>
                <input type="text" class="form-control" name="username" autocomplete="off" />
                <div>评论内容：</div>
                <textarea class="form-control" name="content"></textarea>

                <button type="submit" class="btn btn-primary">发表评论</button>
            </form>
        </div>

        <!-- 评论列表 -->
        <ul class="list-group" id="cmt-list">
            <li class="list-group-item">
                <a href="javaScript:;" class="badge" id='del'>删除</a>
                <span class="badge" style="background-color: #F0AD4E;">评论时间：</span>
                <span class="badge" style="background-color: #5BC0DE;">评论人：</span>
                Item 1
            </li>
        </ul>

    </body>
    <script>
        ////+-*/=-()&^%$#@!~}{|""'':;?/
        function getCommentList() {
            $.ajax({
                type: 'get',
                url: 'http://www.liulongbin.top:3006/api/cmtlist',
                success: function (res) {
                    console.log(res);
                    if (res.status == 200) {
                        var arr = []
                        console.log(1);
                        $.each(res.data, function (i, item) {
                            var str = `
                            <li class="list-group-item">
                                <a href="javaScript:;" class ="badge" data-id = ${item.id} style="background-color:red;">删除</a>
                             <span class="badge" style="background-color: #F0AD4E;">评论时间：${item.time}</span>
                              <span class="badge" style="background-color: #5BC0DE;">评论人：${item.username}</span>
                              ${item.content}
                                  </li>`

                            arr.push(str)
                        });

                        $('#cmt-list').empty().append(arr.join(''))
                    } else {
                        return alert('获取数据失败')
                    }
                }
            })
        }
        getCommentList()

        $('.btn').click(function () {
            $('#formAddCmt').submit(function (e) {
                e.preventDefault()
                let str = $(this).serialize()
                console.log(str);
                $.ajax({
                    type: 'post',
                    url: 'http://www.liulongbin.top:3006/api/addcmt',
                    data: str,
                    success: function (res) {
                        if (res.status === 201) {
                            getCommentList()
                            $('#formAddCmt')[0].reset()
                        } else {
                            return alert('输入有问题')
                        }
                    }
                })

            })
        })
       
        $('ul').on('click', 'a', function () {
            let id = $(this).attr('data-id')
            if (window.confirm('真的要删除么')) {
                $.ajax({
                    type: 'DELETE',
                    url: `http://www.liulongbin.top:3006/api/delcmt?id=${id}`,
                    success: function (res) {
                        if (res.status !== 200) {
                            return alert('链接错误')
                        }
                        getCommentList()
                    }
                })
            }

        })
        var index = 1
        let time = setInterval(function(){
            $.ajax({
                type: 'get',
                url: 'http://www.liulongbin.top:3006/api/cmtlist',
                success: function (res) {
                    
                    console.log(res.data[0].content);
                    if (res.data[0].content == '我出现了'+index+'次') {
                     
                    
                    }else {
                        index++
                        $.ajax({
                    type: 'post',
                    url: 'http://www.liulongbin.top:3006/api/addcmt',
                    data:{
                        username : '计数器',
                        content : '我出现了'+index+'次'
                    } 
                        
                      })
                    }
                    }
                })
                getCommentList()
        },3000)
    </script>
</body>

</html>